home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / packet / thenet / x1j4_src / tnc2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-19  |  10.7 KB  |  423 lines

  1. /*****************************************************************************/
  2. /*                                              */
  3. /*                                         */
  4. /*    *****              *****                      */
  5. /*     *****            *****                         */
  6. /*       *****          *****                         */
  7. /*         *****        *****                         */
  8. /*  ***************      ***************                     */
  9. /*  *****************    *****************                     */
  10. /*  ***************      ***************                     */
  11. /*         *****        *****       TheNet                    */
  12. /*       *****          *****       Portable. Compatible.         */
  13. /*     *****            *****       Public Domain             */
  14. /*    *****              *****    G8KBB                  */
  15. /*                                         */
  16. /* This software is public domain ONLY for non commercial use                */
  17. /*                                                                           */
  18. /*                                         */
  19. /*****************************************************************************/
  20.  
  21. /* 
  22.  * released as TheNet X-1J September 1993
  23.  */
  24.  
  25. #ifdef BANKED
  26. #define EXTERN
  27. #define VARIABLES
  28. #include "all.h"
  29. #include "tntyp.h"
  30. #include "tnl3v.h"
  31. #include "tnl4v.h"
  32. #include "miscv.h"
  33. #include "tnl7av.h"
  34. #ifdef IPROUTE
  35. #include "ip.h"
  36. #include "ipv.h"
  37. #endif
  38.  
  39. char    nulide[] =                /* leerer Ident             */
  40.                    {' ',' ',' ',' ',' ',' '};
  41.  
  42. char    beades[] = { 'I','D',' ',' ',' ',' ','\140' }; /* Call fuer Bake     */
  43. char    cqdest[] = { 'C','Q',' ',' ',' ',' ','\140' }; /* Call fuer CQ-Ruf   */
  44. char    cqdil[]  = "";                    /* Digiliste fuer CQ-Ruf */
  45. char    L4audit[] = "L4    `";
  46. char    L2audit[] = "L2    `";
  47.  
  48. #ifdef MANAGED
  49. char AudCon[] = "C ";
  50. char AudConAcc[] = "CA";
  51. char AudDisc[] = "D ";
  52. char AudDiscAcc[] = "DA";
  53. #endif
  54.  
  55. #ifdef PORTABLE
  56.  
  57. /* The timer() routine is called every 10 milliseconds.
  58.  * The portable version does NOT support the ancilliary
  59.  * functions of l1 stats and cwid
  60.  */
  61.  
  62. VOID timer()
  63. {
  64.     ++tic10;
  65. }
  66.  
  67. /* The stop() routine is used to halt the system upon
  68.  * closedown. WARNING - the portable version does not
  69.  * correctly initialise the SIO - so it may stop with
  70.  * the ptt enabled !!!!!!
  71.  */
  72.  
  73. VOID stop()
  74. {
  75.     DIinc();
  76. loop: goto loop;
  77. }
  78.  
  79. #else
  80. #asm
  81.     .z80
  82. SIADAT    equ    0
  83. SIACTL    equ    1
  84. SIBDAT    equ 2
  85. SIBCTL    equ 3
  86.  
  87.     extrn SIAIW5, SIBIW5
  88.     public stop_
  89. stop_:
  90.     di
  91.     ld    A,5
  92.     out    (SIACTL),A
  93.     ld    A,(SIAIW5)
  94.     out    (SIACTL),A
  95.     ld    A,5
  96.     out    (SIBCTL),A
  97.     ld    A,(SIBIW5)
  98.     out    (SIBCTL),A
  99.     halt
  100.     jr stop_
  101.  
  102.     public timer_
  103.     extrn tic10_, SIAWR5, SIARR0, txon_, dcdon_
  104.     extrn cwtimr_
  105.  
  106. timer_:    
  107.     ld hl,(tic10_)        ; bump the 10 millisecond counter
  108.     inc hl
  109.     ld (tic10_), hl
  110. #endasm
  111. #ifdef STATSCMD
  112. #asm
  113.     ld    a,(SIAWR5)    ; test for RTS on HDLC port, ie PTT is ON
  114.     bit    1,a        ; ( ie the rig is on xmit )
  115.     jr    z,td        ; If not, go try the receiver
  116.     ld    hl,(txon_)    ; If so, bump the TX on counter
  117.     inc    hl
  118.     ld    (txon_),hl    ; Just in case we have digital DCD a la BSX,
  119.     jr    rd        ; skip over RX test coz it is off if TX is on.
  120. td:    ld    a,(SIARR0)    ; Now check the DCD line
  121.     bit    3,a        ; to see if the RX is on
  122.     jr    z,rd        ; If not, skip over
  123.     ld    hl,(dcdon_)    ; If so, bump DCD counter
  124.     inc    hl
  125.     ld    (dcdon_),hl
  126. rd:
  127. #endasm
  128. #endif
  129. #ifdef CWID
  130. #asm
  131.     ld    A,(cwtimr_)    ; ckeck CW timer. Zero means it is not running
  132.     or    A        ; so if it tests zero, just skip over.
  133.     jr    z,timer0
  134.     dec    A        ; If it is not zero, then decrement it and
  135.     ld    (cwtimr_),A    ; store the new value. Also, if it has
  136.     call     Z,cwidbit    ; reached zero, then go get the next bit
  137. #endasm
  138. #endif
  139. #asm
  140. timer0:    ret
  141. #endasm
  142.  
  143. #endif    /* PORTABLE */
  144.  
  145. #endif    /* BANKED */
  146.  
  147. #ifdef CWID
  148. #asm
  149.     .z80
  150.     extrn    cwstate_, cwbit_, cwofs_, cwdat_, cwch_, cwtimr_, cwspeed_
  151.     extrn    xFpar_, Dpar_, HRXRES, HTXSTA, SIAINI, SIAIEN
  152.  
  153.     global calper_,1
  154.     global caltog_,1
  155.     global caltog,1
  156.  
  157. cwidbit:
  158.     ld    HL,cwstate_    ; POINT TO STATE REGISTER
  159.     ld    A,(HL)        ; CHECK CURRENT STATE
  160.     cp    80h        ; if 80h, then we are calibrating
  161.     jp    z, calib
  162.     cp    0        ; if 0, fall thru the next jumps...
  163.     jp    M,cwbitx    ; If '-1', awaiting start of send
  164.     jr    NZ,cwbit2    ; If not zero ( 1 or 2 ) sending a delay
  165.     ld    A,(cwbit_)    ; read current char bits remaining counter
  166.     or    A        ; check if at end ( ie zero )
  167.     jr    NZ,cwbit0    ; if not, continue sending this char
  168.     ld    A,(cwofs_)    ; otherwise we need the next char, so get offs
  169.     inc    A        ; and point to the next character
  170.     ld    (cwofs_),A    ; store new offset for next time
  171.     ld    HL,cwdat_    ; now add offset to start of data table
  172.     ld    E,A
  173.     ld    D,0
  174.     add    HL,DE        ; so HL now points to next data entry
  175.     ld    A,(HL)        ; and read from it the character data,
  176.     and    1fh        ; which is in the lower 5 bits only.
  177.     ld    (cwch_),A    ; and store it in the char holding variable
  178.     ld    A,(HL)        ; get character again for the bits variable
  179.     rla            ; move the contents of the top 3 bits to
  180.     rla            ; the bottom 3 bits of the accumulator
  181.     rla            ; so that the number of bits is in
  182.     rla            ; the low 3 bits of A.
  183.     and    7        ; Now select only the 3 bits
  184.     ld    (cwbit_),A    ; store bit count
  185.     or    A        ; If zero, we are at the end of the callsign
  186.     ld    A,0        ; (just extend the delay if end of character)
  187.     jr    NZ,cwbit7    ; if not end of call, send inter char delay
  188.     ld    A,2        ; signal end of callsign by setting state to 2
  189. cwbit7:    ld    (cwstate_),A    ; and dropping thru into the delay anyway.
  190.     ld    A,(cwspeed_)    ; send a two bit delay period by reading speed
  191.     sla    A        ; doubling it and setting the tick timer
  192.     jr    cwbit3
  193.  
  194. cwbit0:    dec    A        ; continue sending this char, decrement bit
  195.     ld    (cwbit_),A    ; counter and store it
  196.     ld    A,1        ; set state to '1' for next time round
  197.     ld    (HL),A
  198.     ld    HL,cwspeed_    ; point to CWID speed bit value
  199.     ld    A,(cwch_)    ; read current character data
  200.     ld    E,A        ; store it in E temporarily
  201.     bit    0,E        ; check if LSB set. If so DASH not DOT
  202.     ld    A,(HL)        ; set speed as for a dot
  203.     jr    Z,cwbit1    ; and if it was a dash fall thru and treble it
  204.     sla    A        ; by doubling and
  205.     add    A,(HL)        ; adding the original again
  206. cwbit1:    ld    D,A        ; put delay period value in D temorarily
  207.     ld    A,0feh
  208.     out    (SIADAT),A
  209.     ld    A,E        ; get current character data back again
  210.     srl    A        ; move to the next bit
  211.     ld    (cwch_),A    ; and restore the character data
  212.     ld    A,D        ; get delay period back again
  213.     jr    cwbit3
  214.  
  215. cwbit2:    push    AF        ; save state number whilst toggling tone
  216.     ld    A,0feh        ; send a single zero bit to toggle tone
  217.     out    (SIADAT),A    ; by writing out a data byte
  218.     pop    AF        ; restore state
  219.     dec    A        ; A contains state. If it was 1, go to cwbit4
  220.     jr    Z,cwbit4    ; otherwise fall thru to 'stop'
  221. ; cwstop
  222. cwstop:    ld    A,3        ; state 3 means 'i have finished'.
  223.     ld    (cwstate_),A
  224.     ld    HL,SIAINI    ; reset and initialise the channel.
  225.     ld    BC,SIACTL or ((SIAIEN-SIAINI)<<8)
  226.     otir
  227.     xor    A        ; next state of the HDLC Tx is 'idle'
  228.     ld    (HTXSTA),A
  229.     ld    A,5        ; access write register 5
  230.     out    (SIACTL),A
  231.     ld    HL,SIAWR5
  232.     res    1,(HL)        ; and kill the PTT signal
  233.     ld    A,(xFpar_)
  234.     or    A
  235.     jr    NZ,cwbit9
  236.     res    3,(HL)
  237. cwbit9:    ld    A,(HL)
  238.     out    (SIACTL),A
  239.     ld    A,(Dpar_)
  240.     or    A
  241.     call    Z,HRXRES
  242.     jr    cwbitx
  243.  
  244. ; delay sending routine
  245.  
  246. cwbit4:    ld    (HL),A        ; set next state from A register
  247.     ld    A,(cwspeed_)    ; read cw speed value to give single dot delay
  248. cwbit3:    ld    (cwtimr_),A    ; set the timer count to required delay
  249. cwbitx:    ret
  250.  
  251. ; This is the TNC calibrate routine
  252.  
  253. calib:    ld    hl,calper_    ; decrement the 1 second counter
  254.     dec    (hl)        ; If it is zero, then stop sending
  255.     jr    z,cwstop    ; Otherwise, continue another second
  256.     ld    hl,caltog    ; Now decrement the toggle period counter
  257.     dec    (hl)        ; and if it is zero
  258.     jr    nz,calib0
  259.     ld    a,(caltog_)    ; then reset the counter to the start value
  260.     ld    (hl),a
  261.     ld    a,0feh        ; and toggle the o/p tone
  262.     out    (SIADAT),a
  263. calib0:    ld    a,100        ; and wake me in 10ms * 100, ie 1 second
  264.     jr    cwbit3
  265.  
  266. SIACWI:    db    018h,4,0,5,0e2h,3,0d9h,6,0ffh,7,0ffh,1,19h,10h
  267. SIACIX:
  268.  
  269.     public    cwkick
  270.  
  271. cwkick:    ld    A,11
  272.     ld    (HL),A
  273.     ld    HL,SIACWI
  274.     ld    BC,SIACTL or ((SIACIX-SIACWI)<<8)
  275.     otir
  276.     ld    A,5
  277.     out    (SIACTL),A
  278.     ld    A,(SIAWR5)
  279.     res    0,A
  280.     out    (SIACTL),A
  281.     in    A,(SIACTL)
  282.     ld    (SIARR0),A
  283.     ld    A,(cwstate_)    ; see if we are to calibrate or cwid
  284.     cp    -2        ; state of (-2) means calibrate
  285.     jr    nz, cwkik0
  286.     ld    a,(caltog_)    ; initialise toggle period to the value
  287.     ld    (caltog),a    ; set by the sysop
  288.     ld    A,80h        ; set next state to 80h ( calibrating )
  289.     jr    cwkik1
  290. cwkik0:    xor    A
  291. cwkik1:    ld    (cwstate_),A
  292.     ld    A,50
  293.     ld    (cwtimr_),A
  294.     ret
  295.  
  296. #endasm
  297. #endif
  298.  
  299.  
  300. /**************************************************************************\
  301. *                                                                          *
  302. * "copy memory from place to place"                                        *
  303. *                                                                          *
  304. * WARNING - ANSI version handles overlapping memory. this does not         *
  305. *                                                                          *
  306. \**************************************************************************/
  307. #ifndef PORTABLE
  308. #asm
  309.     public memcpy_        ;memcpy(dest,source,count)
  310. memcpy_:
  311.     push    bc        ; save BC register as we use it
  312.     ld    hl,9        ; point to top of params ( count )
  313.     add    hl,sp
  314.     ld    b,(hl)        ; BC contains count
  315.     dec    hl
  316.     ld    c,(hl)
  317.     dec    hl
  318.     ld    d,(hl)        ; DE contains destination for now
  319.     dec    hl
  320.     ld    e,(hl)
  321.     dec    hl
  322.     ld    a,(hl)
  323.     dec    hl
  324.     ld    l,(hl)        ; HL has source address but ....
  325.     ld    h,a
  326.     ex    de,hl        ; swap DE & HL registers
  327.     ldir
  328.     pop    bc
  329.     ret
  330.  
  331. #endasm
  332. #endif
  333.  
  334. /**************************************************************************\
  335. *                                                                          *
  336. * "compare memory at two locations"                                        *
  337. *                                                                          *
  338. * WARNING - assembly versions of cmpid & cmpcal jump INTO this procedure ! *
  339. \**************************************************************************/
  340. #ifndef PORTABLE
  341. #asm
  342.     public compare_        ;compare(id1, id2, count )
  343. compare_:
  344.     push    bc        ; save BC register as we use it
  345.     ld    hl,9        ; point to top of params ( count )
  346.     add    hl,sp
  347.     ld    b,(hl)        ; BC contains count
  348.     dec    hl
  349.     ld    c,(hl)
  350.     dec    hl
  351.     ld    d,(hl)        ; DE contains one address
  352.     dec    hl
  353.     ld    e,(hl)
  354.     dec    hl
  355.     ld    a,(hl)
  356.     dec    hl
  357.     ld    l,(hl)        ; HL has second address
  358.     ld    h,a
  359. c0:    ld    a,(de)
  360.     cpi
  361.     jr    nz, c1
  362.     inc    de
  363.     jp    pe,c0
  364. c4:    ld    hl,1
  365.     jr    c2
  366. c1:    ld    hl,0
  367. c2:    ld    a,h
  368.     or    l
  369.     pop    bc
  370.     ret
  371.  
  372.     public cmpcal_
  373. cmpcal_:    push bc
  374.     ld    hl,7        ; point to top of params ( count )
  375.     add    hl,sp
  376.     ld    d,(hl)        ; DE contains count
  377.     dec    hl
  378.     ld    e,(hl)
  379.     dec    hl
  380.     ld    a,(hl)
  381.     dec    hl
  382.     ld    l,(hl)        ; HL has second address
  383.     ld    h,a
  384.     ld  a,(de)
  385.     cp  32
  386.     jr  z,c1
  387.     ld  a,(hl)
  388.     cp  32
  389.     jr  z,c1
  390.     ld  bc,6
  391.     jr  c0
  392.  
  393.     public cmpid_
  394. cmpid_:    push bc
  395.     ld    hl,7        ; point to top of params ( count )
  396.     add    hl,sp
  397.     ld    d,(hl)        ; DE contains count
  398.     dec    hl
  399.     ld    e,(hl)
  400.     dec    hl
  401.     ld    a,(hl)
  402.     dec    hl
  403.     ld    l,(hl)        ; HL has second address
  404.     ld    h,a
  405.     ld  bc,6
  406. c3:    ld    a,(de)
  407.     cpi
  408.     jr    nz, c1
  409.     inc    de
  410.     jp    pe,c3
  411.     ld  a,(de)
  412.     and 30
  413.     ld  b,a
  414.     ld  a,(hl)
  415.     and 30
  416.     cp  b
  417.     jr  nz,c1
  418.     jr  c4
  419.  
  420.  
  421. #endasm
  422. #endif
  423.